import * as lsp from "vscode-languageserver/node";
import { createLanguageServer } from "./languageServer";
import { createLanguageServiceClient } from "./languageServiceClient";
import { languageServerNotifications, languageServerRequests } from "./types";

export function runLanguageConnection() {
    const connection = lsp.createConnection(lsp.ProposedFeatures.all);
    const client = createLanguageServiceClient(connection);
    const server = createLanguageServer(client);

    connection.onInitialize(server.initialize);
    connection.onInitialized(server.initialized);
    connection.onDidOpenTextDocument(server.didOpenTextDocument);
    connection.onDidChangeTextDocument(server.didChangeTextDocumentContent);
    connection.onDidCloseTextDocument(server.didCloseTextDocument);

    connection.onRequest(languageServerRequests["dsclient/getFileCurrentBranch"], server.getFileCurrentBranch);
    connection.onRequest(languageServerRequests["dsclient/getFileBranches"], server.getFileBranches);
    connection.onRequest(languageServerRequests["dsclient/selectFileBranch"], server.selectFileBranch);
    connection.onRequest(languageServerRequests["dsclient/getProgramMetadata"], server.getProgramMetadata);
    connection.onRequest(languageServerRequests["dsclient/setProgramMetadata"], server.setProgramMetadata);

    connection.onNotification(languageServerNotifications["dsclient/initializeServerWindowConfigurations"], server.initalizeConfigurations);
    connection.onNotification(languageServerNotifications["dsclient/configurationChanged"], server.updateConfiguration);

    connection.onCompletion(server.provideCompletion);
    connection.onHover(server.provideHover);
    connection.onDefinition(server.provideDefinition);
    connection.onReferences(server.provideReferences);
    connection.onRenameRequest(server.provideRename);
    connection.onPrepareRename(server.prepareRename);
    connection.onSignatureHelp(server.provideSignatureHelp);
    connection.onCodeAction(server.provideCodeAction);
    connection.onDocumentSymbol(server.provideDocumentSymbol);
    connection.onDocumentHighlight(server.provideDocumentHighlight);
    connection.onFoldingRanges(server.provideFoldingRanges);
    connection.onDidChangeWatchedFiles(server.didChangeWatchedFiles);
    connection.languages.semanticTokens.on(server.provideSemanticTokens);
    connection.languages.semanticTokens.onRange(server.provideSemanticTokensRange);

    connection.listen();
}

